#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket  # 导入 socket 模块
import json
import requests
import os
import time

def send(msg):
    client = socket.socket()  # 创建 socket 对象
    host = socket.gethostname()  # 获取本地主机名
    port = 12345  # 设置端口号
    client.connect((host, port))
    msg = json.dumps(msg)
    client.send(msg.encode('utf-8'))
    data = client.recv(1024)
    print('recv:', data.decode())
    client.close()
    return data.decode()

def upLoad(taskId):
    file_name = '';
    day = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    fq = "..\\screenShots\\" + day
    tm = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime(time.time()))
    type = '.png'
    filename = ""
    if os.path.exists(fq):
        filename = fq + "\\" + taskId + type
    else:
        os.makedirs(fq)
        filename = fq + "\\" + taskId + type

    url = 'http://192.168.1.88:8123/dd/submit/task'
    files = {'file': open(filename, 'rb') }
    r = requests.post(url, files=files, data={
        'taskId': taskId
    })
    print(r.text)

if __name__ == '__main__':
    requests.get('http://192.168.1.88:8123/dd/get/token')
    a = requests.get('http://192.168.1.88:8123/dd/pull/one');
    d = a.json();
    d = d['data']
    m = send(d)
    if(m): upLoad(d['id'])

    # upLoad('1252878891841171457')
